//
//  MyViewController.m
//  Router
//
//  Created by teki on 15/10/15.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "MyViewController.h"
#import "SystemSettingTableViewCell.h"

#import "MyHeaderView.h"

#import "MyWIFIListViewController.h"
#import "ForgetPasswordViewController.h"
#import "LoginResponseModel.h"
#import "AppDelegate.h"
#import "SettingNickNameViewController.h"

@interface MyViewController ()
@property (nonatomic, strong) MyHeaderView *headerView;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.navigationItem.title = @"我";
    
    [self.tableView registerClass:[SystemSettingTableViewCell class] forCellReuseIdentifier:@"SystemSettingTableViewCell"];
    self.tableView.separatorColor = SeparatorLineColor;
   
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = [UIView new];
    
 
    
    UIButton *logoutBtn = [self.view addButtonWithTitle:@"注  销" target:self action:@selector(logout:)];
    [logoutBtn setBackgroundImage:[UIImage imageWithColor:NavgationBarBackgroundColor] forState:UIControlStateNormal];
    [logoutBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(23);
        make.right.equalTo(-23);
        make.height.equalTo(45);
        make.bottom.equalTo(self.view.bottom).offset(-100);
    }];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app名称
    //NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    // app build版本
    //NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    UILabel *versionMessageLabel = [UILabel new];
    [self.view addSubview:versionMessageLabel];
    [versionMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.bottom).offset(-30);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
    }];
    versionMessageLabel.font = [UIFont systemFontOfSize:13];
    versionMessageLabel.textColor = [UIColor lightGrayColor];
    versionMessageLabel.textAlignment = NSTextAlignmentCenter;
    versionMessageLabel.text = [NSString stringWithFormat:@"当前应用版本：%@",app_Version];
 
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    LoginResponseModel *model =  [UserSingleton shareInstance].userModel;
    UserModel *user = model.user[0];
    self.headerView.model = user;
}




#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SystemSettingTableViewCell *cell = (SystemSettingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SystemSettingTableViewCell" forIndexPath:indexPath];
    [cell configMyViewCellWith:nil andIndexPath:indexPath];
    return cell;
}


#pragma mark -
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [self pushViewControllerWithClassName:NSStringFromClass([MyWIFIListViewController class])];
    }
    else if (indexPath.row ==1)
    {
        [self pushViewControllerWithClassName:NSStringFromClass([SettingNickNameViewController class])];
    }else{
        ForgetPasswordViewController *forgetVC = [[ForgetPasswordViewController alloc] init];
        forgetVC.controllerType = ViewControllerTypeModifyPassword;
        [self.navigationController pushViewController:forgetVC animated:YES];
    }

}


#pragma mark -
#pragma mark
- (void)logout:(UIButton *)sender
{
    [UserSingleton shareInstance].password = nil;
    UserModel *user = [UserSingleton shareInstance].userModel.user[0];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:user.userName];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    self.activity = [self startActivityWithTitle:@"正在退出!"];
    [self dispatchTimerWithTime:2.0 block:^{
        [self stopActivityWithActivity:self.activity title:nil];
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *mainVC = [main instantiateViewControllerWithIdentifier:@"LoginVC"];
        ApplicationDelegate.window.rootViewController = mainVC;
    }];

}



#pragma mark -
#pragma mark    setters and getters
- (MyHeaderView *)headerView{
    if (_headerView ==nil)
    {
        self.headerView = [[MyHeaderView alloc] initWithFrame:Rect(0, 0, ScreenWidth, 100)];
    }
    return _headerView;
}


@end
